package com.example.a22310.myapplication.ViewModel.Post;


import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.Toast;

import com.example.a22310.myapplication.Activity.UserPost;
import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Model.PostModel;
import com.example.a22310.myapplication.Third.SpacesItemDecoration;
import com.example.a22310.myapplication.databinding.ActivityUserPostBinding;
import com.example.a22310.myapplication.databinding.FragmentPostBinding;

import java.util.List;

public class PostViewModel  implements PostListener<Post> {
    private FragmentPostBinding binding;
    private FragmentActivity activity;



    public PostViewModel(FragmentActivity activity, FragmentPostBinding binding){
        this.activity = activity;
        this.binding = binding;
        binding.setPost(this);
        PostModel postModel=new PostModel();
        postModel.postList(PostViewModel.this);
    }

    public PostViewModel(FragmentActivity userPost, FragmentPostBinding binding) {
    }

    public PostViewModel(int userPost, int binding) {
    }

    public PostViewModel(UserPost userPost, int userid, ActivityUserPostBinding binding) {
    }

    @Override
    public void onSuccess(Post post) {
        if(post.getCode()!=404){
            List<Post.PostlistBean> postlistBeen =  post.getPostlist();

//            fragmentClubInroBinding.recyclerViewCoach.setLayoutManager(
//                    new LinearLayoutManager(activity));

            binding.recyclerViewPost.setLayoutManager(new StaggeredGridLayoutManager(2,
                    StaggeredGridLayoutManager.VERTICAL));

//            binding.recyclerViewPost.setLayoutManager(
//                    new LinearLayoutManager(activity));
            MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post,
                    postlistBeen, BR.post);
            binding.recyclerViewPost.setAdapter(adapter);
            SpacesItemDecoration decoration=new SpacesItemDecoration(16);
            binding.recyclerViewPost.addItemDecoration(decoration);


        }

    }

    @Override
    public void onFailure() {
        Toast.makeText(activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }
}
